package ch.amerkle.sender;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public class CoopSender {
        public static boolean dev = false;
        private static String host = "http://coop.nth.ch";
        private static String loginUrl = "/index/login/lang/de";
        private static String contactsUrl = "/contacts/index/lang/de";
        private FileWriter fw = null;
        
        private String user = null;
        private String pw = null;
        
        private static String ACTION_LISTADDRESSBOOK    = "listaddressbook";
        private static String ACTION_SENDSMS                    = "sendsms";
        private static String ACTION_LISTORIGINATOR             = "listoriginators";
        private static String ACTION_DELETEADDRESS              = "deleteaddress";
        private static String ACTION_ADDADDRESS                 = "addaddress";
        private static String ACTION_MODIFIYADDRESS             = "modifyaddress";
        
        private static String PARAM_ACTION                              = "action";
        private static String PARAM_USERNAME                    = "username";
        private static String PARAM_PASSWORD                    = "password";
        private static String PARAM_MESSAGE                             = "message";
        private static String PARAM_NUMBER                              = "number";
        private static String PARAM_ORIGINATOR                  = "originator";
        private static String PARAM_NAME                                = "name";
        private static String PARAM_ROWID                               = "rowid";
        
        private static String ORIGINATOR_PHONE = "phone";
        private static String ORIGINATOR_AUTO  = "auto";
        
        private String statusString = null;
        private int statusCode;

        public CoopSender(String user, String pass){
                System.err.close();
                this.user = user;
                this.pw = pass;
                File file = new File("C:\\Users\\triboni\\Downloads\\cs.log");
                try {
					fw = new FileWriter(file);
				} catch (IOException e) {
					e.printStackTrace();
				}
        }
        
        public boolean login(){
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(host+loginUrl);
            try{
                    method.addParameter("mail", user);
                    method.addParameter("password", pw);
                    
                    client.executeMethod(method);
                    boolean ok = method.getStatusCode() == 302;
                    if(ok){
                    	System.out.println("Login ok.");
                    } else {
                    	System.out.println("Login failed.");
                    }
                    
                    fw.close();
                    
                    return ok;
            } catch (Exception e) {
                    if(dev)
                            e.printStackTrace();
            }
            return false;
        }
        
        public int getStatusCode(){
                return statusCode;
        }
        
        public String getStatusString(){
                return statusString;
        }
        
        public ArrayList<AddressBookEntry> listAddressBook(){
                HttpClient client = new HttpClient();
                PostMethod method = new PostMethod(host+loginUrl);
                BufferedReader br = null;
                String line;
                try{
                        method.addParameter(PARAM_ACTION, ACTION_LISTADDRESSBOOK);
                        method.addParameter(PARAM_USERNAME, user);
                        method.addParameter(PARAM_PASSWORD, pw);
                        
                        client.executeMethod(method);
                        if(dev)
                                System.out.println(method.getResponseBodyAsString());
                        br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
                        
                        String[] values;
                        ArrayList<AddressBookEntry> addresses = new ArrayList<AddressBookEntry>();
                        line = br.readLine();
                        this.statusCode = method.getStatusCode();
                        this.statusString = method.getStatusText();
                        if(this.statusCode != 200) return null;

                        while((line = br.readLine())!= null){
                                line = line.replaceAll("\"", "");
                                values = line.split(",");
                                addresses.add(new AddressBookEntry(values[0], values[1], values[2], Integer.parseInt(values[3])));
                        }
                        
                        
                        
                        return addresses;
                } catch (Exception e) {
                        if(dev)
                                e.printStackTrace();
                }
                return null;
        }
        
        public boolean sendSms(String message, String number, boolean originatorPhone){
                HttpClient client = new HttpClient();
                PostMethod method = new PostMethod(null);
                
                try{
                        method.addParameter(PARAM_ACTION, ACTION_SENDSMS);
                        method.addParameter(PARAM_USERNAME, user);
                        method.addParameter(PARAM_PASSWORD, pw);
                        method.addParameter(PARAM_MESSAGE, message);
                        method.addParameter(PARAM_NUMBER, number);
                        if(originatorPhone)
                                method.addParameter(PARAM_ORIGINATOR, ORIGINATOR_PHONE);
                        else
                                method.addParameter(PARAM_ORIGINATOR, ORIGINATOR_AUTO);
                        
                        
                        client.executeMethod(method);
                        byte[] responseBody = method.getResponseBody();
                        this.statusCode = method.getStatusCode();
                        this.statusString = method.getStatusText();
                        
                        if(dev)
                                System.out.println(new String(responseBody));
                        if(this.statusCode != 200){
                                return false;
                        } else {
                                return true;
                        }
                } catch (Exception e) {
                        if(dev)
                                e.printStackTrace();
                }
                return false;
        }
        
        public boolean addAddress(String name, String number){
                HttpClient client = new HttpClient();
                PostMethod method = new PostMethod(null);
                
                try{
                        method.addParameter(PARAM_ACTION, ACTION_ADDADDRESS);
                        method.addParameter(PARAM_USERNAME, user);
                        method.addParameter(PARAM_PASSWORD, pw);
                        method.addParameter(PARAM_NAME, name);
                        method.addParameter(PARAM_NUMBER, number);
                        
                        client.executeMethod(method);
                        byte[] responseBody = method.getResponseBody();
                        this.statusCode = method.getStatusCode();
                        this.statusString = method.getStatusText();
                        
                        if(dev)
                                System.out.println(new String(responseBody));
                        if(this.statusCode != 200){
                                return false;
                        } else {
                                return true;
                        }
                } catch (Exception e) {
                        if(dev)
                                e.printStackTrace();
                }
                return false;
        }
        
        public boolean modifyaddress(String id, String name, String number){
                HttpClient client = new HttpClient();
                PostMethod method = new PostMethod(null);
                
                try{
                        method.addParameter(PARAM_ACTION, ACTION_MODIFIYADDRESS);
                        method.addParameter(PARAM_USERNAME, user);
                        method.addParameter(PARAM_PASSWORD, pw);
                        method.addParameter(PARAM_ROWID, id);
                        if(name != null && name.length()>0)
                                method.addParameter(PARAM_NAME, name);
                        if(number != null && number.length()>0)
                                method.addParameter(PARAM_NUMBER, number);
                        
                        client.executeMethod(method);
                        byte[] responseBody = method.getResponseBody();
                        this.statusCode = method.getStatusCode();
                        this.statusString = method.getStatusText();
                        
                        if(dev)
                                System.out.println(new String(responseBody));
                        if(this.statusCode != 200){
                                return false;
                        } else {
                                return true;
                        }
                } catch (Exception e) {
                        if(dev)
                                e.printStackTrace();
                }
                return false;
        }

        public boolean deleteaddress(String id){
                HttpClient client = new HttpClient();
                PostMethod method = new PostMethod(null);
                
                try{
                        method.addParameter(PARAM_ACTION, ACTION_DELETEADDRESS);
                        method.addParameter(PARAM_USERNAME, user);
                        method.addParameter(PARAM_PASSWORD, pw);
                        method.addParameter(PARAM_ROWID, id);
                        
                        client.executeMethod(method);
                        byte[] responseBody = method.getResponseBody();
                        this.statusCode = method.getStatusCode();
                        this.statusString = method.getStatusText();
                        
                        if(dev)
                                System.out.println(new String(responseBody));
                        if(this.statusCode != 200){
                                return false;
                        } else {
                                return true;
                        }
                } catch (Exception e) {
                        if(dev)
                                e.printStackTrace();
                }
                return false;
        }
}
